<template>
  <div class="login-dialog">
    <!-- 遮罩屏背景 -->
    <div class="overlay"></div>
    <div class="login-box">
      <div class="container">
        <div class="close" @click="close">X</div>
        <div class="title">
          <p>微信扫码关注公众号</p>
          <p>发送"验证码"即可获取验证码登录</p>
        </div>
        <div class="qr">
          <img src="@/assets/qr.jpg" alt="" />
        </div>
        <div class="wrapper">
          <el-input placeholder="请输入验证码" v-model="validCode"></el-input>
          <el-button
            type="primary"
            plain
            @click="login"
            style="margin-left: 10px"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import useUserStore from "@/store/modules/user";
import time from "@/utils/time";
import { ElMessage } from "element-plus";

const props = defineProps(["close"]);

let userStore = useUserStore();

// 验证码
let validCode = ref("");

// 登录
const login = async () => {
  if (!validCode.value.trim()) {
    ElMessage.warning("请输入验证码");
    return;
  }

  userStore
    .userLogin(validCode.value)
    .then((res) => {
      setTimeout(() => {
        props.close();
        // 登录成功的提示信息
        ElNotification({
          type: "success",
          message: "登录成功",
          title: `Hi ${time()}`,
        });
      }, 0);
    })
    .catch((error) => {
      // 登录失败的提示信息
      ElNotification({
        type: "error",
        message: "登录失败",
      });
    });
};

// 关闭遮罩层
const close = () => {
  props.close();
};
</script>

<style scoped lang="scss">
.login-dialog {
  width: 100%;
  height: 100%;
  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 998;
  }
  .login-box {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 998;
    .container {
      width: 400px;
      height: 500px;
      background-color: rgba(255, 255, 255, 0.4);
      display: flex;
      flex-direction: column;
      align-items: center;
      border-radius: 20px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.132);
      backdrop-filter: blur(10px);
      .close {
        position: absolute;
        right: 10px;
        top: 10px;
        color: #fff;
        width: 25px;
        height: 25px;
        border-radius: 50%;
        background-color: #ccc;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        font-weight: bold;
      }

      .title {
        color: #ffffff;
        text-align: center;
        margin: 20px 0 0 0;
      }
      .qr {
        width: 100%;
        display: flex;
        justify-content: center;
        margin: 10px 0;
        img {
          width: 60%;
        }
      }
      .wrapper {
        width: 65%;
        margin-top: 10px;
        display: flex;
        justify-content: space-around;
      }
    }
  }
}
</style>